package com.liusy.code.wizards;

import org.eclipse.jdt.internal.core.PackageFragment;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import com.liusy.code.bean.PackageNameInfo;

/**
 * 包名配置
 * 
 * @author liusy
 *
 */
@SuppressWarnings( { "restriction" })
public class CodeGenerationWizardPage extends AbsWizardPage
{
	private Text entityPackageText;//dao包名
	private Text daoPackageText;//dao包名
	private Text servicPackageText; //service包名
	private Text actionPackageText;//action包名
	private Text jspPageeText;//jsp页面路径
	private PackageNameInfo packageNameInfo;
	

	private ISelection selection;

	protected CodeGenerationWizardPage(ISelection selection)
	{
		super("Code");
		setTitle("代码生成");
		this.selection = selection;
	}

	@Override
	public void createControl(Composite parent)
	{
		//创建一个新的Composite
		Composite container = new Composite(parent, SWT.NULL);
		//设置网格布局
		GridLayout layout = new GridLayout();
		container.setLayout(layout);
		layout.numColumns = 2;//2列
		layout.verticalSpacing = 9;//垂直间距
		//---------------------------------------------------------------
		Label label = new Label(container, SWT.NULL);
		label.setText("entity包路径:");
		entityPackageText = new Text(container, SWT.READ_ONLY | SWT.BORDER | SWT.SINGLE);
		entityPackageText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		//---------------------------------------------------------------
		label = new Label(container, SWT.NULL);
		label.setText("dao包路径:");
		daoPackageText = new Text(container, SWT.READ_ONLY | SWT.BORDER | SWT.SINGLE);
		daoPackageText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		//---------------------------------------------------------------
		label = new Label(container, SWT.NULL);
		label.setText("service包路径:");
		servicPackageText = new Text(container, SWT.READ_ONLY | SWT.BORDER | SWT.SINGLE);
		servicPackageText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		//---------------------------------------------------------------
		label = new Label(container, SWT.NULL);
		label.setText("action包路径:");
		actionPackageText = new Text(container, SWT.READ_ONLY | SWT.BORDER | SWT.SINGLE);
		actionPackageText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		//---------------------------------------------------------------
		label = new Label(container, SWT.NULL);
		label.setText("jsp页面路径:");
		jspPageeText = new Text(container,  SWT.BORDER | SWT.SINGLE);
		jspPageeText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		jspPageeText.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent arg0) {
				dialogChanged();
			}
		});
		//---------------------------------------------------------------
		//设置焦点
		parent.setFocus();
		//初始化界面默认值
		initialize();

		dialogChanged();

		setControl(container);
	}

	/**
	 * 初始化界面默认值
	 */
	protected void initialize()
	{
		if (selection != null && selection.isEmpty() == false && selection instanceof IStructuredSelection)
		{
			IStructuredSelection ssel = (IStructuredSelection) selection;

			Object obj = ssel.getFirstElement();
			if (obj instanceof PackageFragment)
			{
				PackageFragment packageFragment = (PackageFragment) obj;
				packageNameInfo = new PackageNameInfo(packageFragment);
				entityPackageText.setText(packageNameInfo.getEntityPackageName());
				daoPackageText.setText(packageNameInfo.getDaoPackageName());
				servicPackageText.setText(packageNameInfo.getServicPackageName());
				actionPackageText.setText(packageNameInfo.getActionPackageName());
				jspPageeText.setText(packageNameInfo.getJspPath());
				setDescription("模块“"+packageNameInfo.getModeName()+"”相应的包名设置");
			}
		}
	}
	
	@Override
	protected Object getResult() {
		return packageNameInfo;
	}

	
	/**
	 * 验证
	 */
	private void dialogChanged()
	{
		String jspPage = jspPageeText.getText();
		packageNameInfo.setJspPath(jspPageeText.getText());
		if (jspPage.indexOf("/")!=0||!jspPage.substring(jspPage.lastIndexOf("/")+1,jspPage.length()).equals(packageNameInfo.getModeName()))
		{
			updateStatus("JSP页面路径前需要加项目根路径“/src/main/webapp/web/"+packageNameInfo.getModeName());
			return;
		}
		updateStatus(null);
	}

}
